<?php
namespace app\wr\controller;
use app\common\controller\WrBase;
require VENDOR.'/PhpExcel/PHPExcel.class.php';
require VENDOR.'/PhpExcel/PHPExcel/IOFactory.php';

class Excel extends WrBase{

	/*
	 * 商户信息导出
	 * */
	public function companyExcel($data = array()){

		$objPHPExcel=new \PHPExcel();//实例化PHPExcel类， 等同于在桌面上新建一个excel
		$objPHPExcel->createSheet();
		$objPHPExcel->setActiveSheetIndex();
		$objSheet=$objPHPExcel->getActiveSheet();
		$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);

		$objSheet->setTitle('商户信息');
		$objSheet->setCellValue("A1","商户号")->setCellValue("B1","商户名称")->setCellValue("C1","手机号")->setCellValue("D1","邮箱")->setCellValue("E1","店铺数")->setCellValue("F1","理发师数量")->setCellValue("G1","短信数量")->setCellValue("H1","商户状态")->setCellValue("I1","备注")->setCellValue("J1","时间");
		$j=2;
		foreach($data as $key=>$val){
			$objSheet->setCellValue("A".$j,$val['cId'])->setCellValue("B".$j,$val['cName'])->setCellValue("C".$j,$val['tel'])->setCellValue("D".$j,$val['email'])->setCellValue("E".$j,$val['shopNum'])->setCellValue("F".$j,$val['barberNum'])->setCellValue("G".$j,$val['msgNum'])->setCellValue("H".$j,$val['status'])->setCellValue("I".$j,$val['remark'])->setCellValue("J".$j,$val['time']);
			$j++;
		}
		$objWriter=\PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5');//生成excel文件
		//$objWriter->save($dir."/export_1.xls");//保存文件
		$this->browser_export('Excel5','商户信息'.date("Y-m-d",time()).'xls');//输出到浏览器
		$objWriter->save("php://output");
	}


	/*
 * 商户信息导出
 * */
	public function adminExcel($data = array()){

		$objPHPExcel=new \PHPExcel();//实例化PHPExcel类， 等同于在桌面上新建一个excel
		$objPHPExcel->createSheet();
		$objPHPExcel->setActiveSheetIndex();
		$objSheet=$objPHPExcel->getActiveSheet();
		$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);

		$objSheet->setTitle('管理员信息');
		$objSheet->setCellValue("A1","账号")->setCellValue("B1","手机号")->setCellValue("C1","邮箱")->setCellValue("D1","备注")->setCellValue("E1","用户组")->setCellValue("F1","开户时间")->setCellValue("G1","状态");
		$j=2;
		foreach($data as $key=>$val){
			$objSheet->setCellValue("A".$j,$val['adminName'])->setCellValue("B".$j,$val['tel'])->setCellValue("C".$j,$val['email'])->setCellValue("D".$j,$val['remark'])->setCellValue("E".$j,$val['title'])->setCellValue("F".$j,$val['registrTime'])->setCellValue("G".$j,$val['status']);
			$j++;
		}
		$objWriter=\PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5');//生成excel文件
		//$objWriter->save($dir."/export_1.xls");//保存文件
		$this->browser_export('Excel5','管理员信息'.date("Y-m-d",time()).'xls');//输出到浏览器
		$objWriter->save("php://output");
	}

	function browser_export($type,$filename){
		if($type=="Excel5"){
			header('Content-Type: application/vnd.ms-excel');//告诉浏览器将要输出excel03文件
		}else{
			header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告诉浏览器数据excel07文件
		}
		header('Content-Disposition: attachment;filename="'.$filename.'"');//告诉浏览器将输出文件的名称
		header('Cache-Control: max-age=0');//禁止缓存
	}


}